package com.hualan.controller;

import com.hualan.service.FMService1;
import com.hualan.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/fm")
public class FMController1 {

    @Autowired private FMService1 fmService;
    @GetMapping("/selectAll")
    public R selectAll(){
        return fmService.selectAll();
    }
    @GetMapping("/update")
    public R update(Long id,String name,String music,String picture){
        return fmService.update(id,name,music,picture);
    }
    @GetMapping("/insert")
    public R insert(String name,String music,String picture){
        return fmService.insert(name,music,picture);
    }


    @GetMapping("/delete")
    public R delete(Long id){
        return fmService.delete(id);
    }
//基于selectAll实现分页查询
    @GetMapping("/selectAllByPage")
    public R selectAllByPage(Integer page, Integer size){
        return fmService.selectAllByPage(page,size);
    }
    @GetMapping("/totalPage")
    public R totalPage(){
        return fmService.totalPage();
    }
    @GetMapping("/selectLikeName")
    public R selectLikeName(String name){
        return fmService.selectLikeName(name);
    }

    @PostMapping("/upload")
    public R upload(MultipartFile file) throws IOException {
        String directoryPath = "d:/upload";
        File directory = new File(directoryPath);
        if (!directory.exists()) {
            boolean created = directory.mkdirs();
            if (created) {
                System.out.println("目录创建成功：" + directoryPath);
            } else {
                System.out.println("目录创建失败。");
            }
        } else {
            System.out.println("目录已存在。");
        }


      String  path="D:\\upload\\";

      String getOriginalFilename = file.getOriginalFilename();
      String ext= getOriginalFilename.substring(getOriginalFilename.lastIndexOf("."));
      String filename = UUID.randomUUID().toString()+ext;
      path+=filename;
      file.transferTo(new File(path));
      return R.success(filename);
    }
    @RequestMapping("/down")
    public void down(String filename, HttpServletResponse resp) throws IOException {
        String directoryPath = "d:/upload";
        File directory = new File(directoryPath);
        if (!directory.exists()) {
            boolean created = directory.mkdirs();
            if (created) {
                System.out.println("目录创建成功：" + directoryPath);
            } else {
                System.out.println("目录创建失败。");
            }
        } else {
            System.out.println("目录已存在。");
        }


        String path="d:\\upload\\"+filename;
//        读取文件内容，写入响应体中
        FileInputStream in=new FileInputStream(path);
        ServletOutputStream out= resp.getOutputStream();
        byte[] b=new byte[1024];
        int i = in.read(b);
        while (i!=-1){
            out.write(b,0,i);
            i=in.read(b);
        }
        out.close();
        in.close();

    }

}
